/*
"JSFXGEN" - ToolTip.as
Modular Integrated Development Environment for JesuSonic
Project page: http://jsfxgen.googlecode.com

Copyright 2009 Lubomir I. Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package neolit123.ui
{
  import flash.display.Sprite;
  import flash.display.DisplayObject;
  import flash.text.TextField;
  import flash.text.TextFormat;
  import flash.utils.Timer;
  import flash.events.MouseEvent;
  import flash.events.TimerEvent;
  // =========================================================
  public class ToolTip extends Sprite
  {
    private var tf:TextFormat = new TextFormat("_sans",10,0x000000,true,null,null,null,null,null,3,1);
    private var txt:TextField = new TextField();    
    private var tgt:DisplayObject;
    private var movedone:Boolean = false;
    public var box:Box = new Box(15,15,0xffffff,0x000000,true,3);
    // =========================================================
    // [ create tooltip
    // =========================================================
    public function ToolTip():void
    {
      // @ add text & box
      addChild(box);
      addChild(txt);
      // @ set text
      with (txt)
      {
        width = height = 1;
        autoSize = "left";
        selectable = false;
        text = "notextyet";
      }
      // @ set box
      box.setsize(null,txt.height-2);
      visible = false;
    }
    // =========================================================
    // [ add item
    // =========================================================
    public function add(tgt:DisplayObject, str:String):void
    {
      // @ set mouse over event
      tgt.addEventListener(MouseEvent.MOUSE_OVER, mctipshow);
      function mctipshow(e:MouseEvent):void
      {
        show(str,e);
      }
      // @ set mouse out event
      tgt.addEventListener(MouseEvent.MOUSE_OUT, mctiphide);
      function mctiphide(e:MouseEvent=null):void
      {
        hide();
      }
      tgt.addEventListener(MouseEvent.MOUSE_MOVE, mctipmove);
      function mctipmove(e:MouseEvent=null):void
      {
        move(e.stageX, e.stageY);
      }
    }
    // =========================================================
    // [ show tooltip
    // =========================================================
    public function show(str:String,e:MouseEvent):void
    {
      move(e.stageX, e.stageY);
      txt.text = str;
      txt.setTextFormat(tf);
      box.setsize(txt.width,null);
      visible = true;
    }
    // =========================================================
    // [ hide tooltip
    // =========================================================
    public function hide():void
    {
      visible = false;
    }
    // =========================================================
    // [ move tooltip
    // =========================================================
    public function move(tx:Number, ty:Number):void
    {
      if (tx > stage.stageWidth-txt.width-40) tx -= txt.width+20 else tx += 20;
      if (ty < 20) ty = 0 else ty -= 20;
      x = tx;
      y = ty;
    }
  }
}